package com.lingx.web.api.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.service.LingxService;
import com.lingx.web.api.IApi;
@Component
public class Api8011 extends AbstractApi implements IApi{
	@Resource
	private LingxService lingxService;
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int getApiCode() {
		return 8011;
	}
	@Override
	public String getApiName() {
		return "接收功能菜单";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String funcJson=IApi.getParamString(params, "funcJson", "");
		String menuJson=IApi.getParamString(params, "menuJson", "");
		String secret1=IApi.getParamString(params, "secret", "");
		String enabled=this.lingxService.getConfigValue("lingx.update.enabled", "none");
		String secret2=this.lingxService.getConfigValue("lingx.update.secret", "none");
		if("true".equals(enabled)&&secret2.equals(secret1)) {
			List<Map<String,Object>> list=(List<Map<String,Object>>)JSON.parse(funcJson);
			this.func(list);
			
			list=(List<Map<String,Object>>)JSON.parse(menuJson);
			this.menu(list);
		}
		return ret;
	}
	
public void func(List<Map<String,Object>> list) {
		
		String sql = "select count(*) from tlingx_func where module=? and func=?";
		String module,func;
		for(Map<String,Object> map:list) {
			module=map.get("module").toString();
			func=map.get("func").toString();
			if(this.jdbcTemplate.queryForObject(sql, Integer.class,module,func)==0) {
				this.jdbcTemplate.update("insert into tlingx_func(id,name,module,func,type,fid)values(?,?,?,?,?,?)",
						map.get("id"),map.get("name"),map.get("module"),map.get("func"),map.get("type"),map.get("fid"));
				this.jdbcTemplate.update("INSERT into tlingx_role_func(id,role_id,func_id)select uuid(),'"+Api9016.SUPER_ADMIN_ID+"',id from tlingx_func t where not EXISTS (select 1 from tlingx_role_func where role_id='"+Api9016.SUPER_ADMIN_ID+"' and func_id=t.id)");
				
			}
		}
	}

	public void menu(List<Map<String,Object>> list) {
		String id,name,fid;
		String sql = "select count(*) from tlingx_menu where id=? or name=?";
		for(Map<String,Object> map:list) {
			id=map.get("id").toString();
			fid=map.get("fid").toString();
			name=map.get("name").toString();
			if(this.jdbcTemplate.queryForObject(sql, Integer.class,id,name)==0) {
				this.jdbcTemplate.update("insert into tlingx_menu(id,name,short_name,type,icon_cls,fid,orderindex,status,state,func_id,remark) values(?,?,?,?,?,?,?,?,?,?,?)",id,name,map.get("short_name"),map.get("type"),map.get("icon_cls"),fid,map.get("orderindex"),map.get("status"),map.get("state"),map.get("func_id"),map.get("remark"));

				this.jdbcTemplate.update("insert into tlingx_role_menu(id,menu_id,role_id) select uuid(),t.id,a.role_id from tlingx_menu t,tlingx_role_menu a where a.menu_id=t.fid and fid=? and not exists (select 1 from tlingx_role_menu where menu_id=t.id)",fid);
			}else {
				this.jdbcTemplate.update("update tlingx_menu set name=?,func_id=? where id=?",name,map.get("func_id"),id);//status=?,,map.get("status")
			}
		}
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
}
